import { createRouter, createWebHashHistory } from 'vue-router'
import login from '@/views/login/login.vue'
import frame from '@/views/main/frame.vue'
import { useAuthStore } from '@/stores/auth'
import myabsent from '@/views/absent/my.vue'
import subabsent from '@/views/absent/sub.vue'
import publish from '@/views/inform/publish.vue'
import inform_detail from '@/views/inform/detail.vue'
import inform_list from '@/views/inform/list.vue'
import staffadd from '@/views/staff/add.vue'
import stafflist from '@/views/staff/list.vue'


const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'frame',
      component: frame,
      children: [
        {path:'/absent/my',name:'myabsent',component:myabsent},
        {path:'/absent/sub',name:'subabsent',component:subabsent},
        {path:'/inform/publish',name:'inform_publish',component:publish},
        {path:'/inform/list',name:'inform_list',component:inform_list},
        {path:'/inform/detail/:pk',name:'inform_detail',component:inform_detail},
        {path:'/staff/add',name:'staff_add',component:staffadd},
        {path:'/staff/list',name:'staff_list',component:stafflist}

      ]
    },
    {
      path: '/login',
      name: 'login',
      component: login
    }
  ]
})

// 使用钩子函数去校验是否登录
router.beforeEach((to,from)=>{
  // 判断用户是否登录 未登录重定向到登录页面
  const authStore = useAuthStore()
  if(!authStore.is_logined && to.name != 'login'){
    return {name:'login'}
  }
})

export default router
